unit gastoEmpresa;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Provider, SqlExpr, DB, DBClient, DBLocal, DBLocalS, StdCtrls,
  Buttons, Grids, DBGrids, RXDBCtrl, Mask, ToolEdit, DateUtils, DBCtrls;

type
  TfrmGastosEmpresa = class(TForm)
    edtFechaIni: TDateEdit;
    edtFechaFin: TDateEdit;
    lblFechaIni: TLabel;
    lblFechaFin: TLabel;
    grdMaterial: TRxDBGrid;
    btnExcel: TSpeedButton;
    btnVistaPrevia: TBitBtn;
    btnCargar: TBitBtn;
    cds: TSQLClientDataSet;
    s: TDataSource;
    cdsGASTO: TStringField;
    cdsFECHA: TSQLTimeStampField;
    cdsCOMPROBANTE: TStringField;
    cdsMONTO: TFloatField;
    chbTodos: TCheckBox;
    Label5: TLabel;
    edtFiltro: TEdit;
    btnDesfiltrar: TBitBtn;
    cdstotal: TAggregateField;
    Label3: TLabel;
    total: TDBText;
    procedure btnCargarClick(Sender: TObject);
    procedure grdMaterialTitleClick(Column: TColumn);
    procedure edtFiltroChange(Sender: TObject);
    procedure btnExcelClick(Sender: TObject);
    procedure btnDesfiltrarClick(Sender: TObject);
    procedure chbTodosClick(Sender: TObject);
    procedure edtFechaIniChange(Sender: TObject);
  private
    { Private declarations }
  public
    constructor CreateNuevo(AOwner: TComponent; fechaInicial, fechaFinal: TDateTime; todos: boolean);

  end;

var
  frmGastosEmpresa: TfrmGastosEmpresa;

implementation

uses Recursos;

{$R *.dfm}

procedure TfrmGastosEmpresa.btnCargarClick(Sender: TObject);
begin
  self.cds.Close;
  self.cds.Params[1].AsString := FormatDateTime('mm/dd/yy', incDay(StrToDate(self.edtFechaFin.EditText)));
  if self.chbTodos.Checked then
    self.cds.Params[0].AsString := '01/01/1900'
  else
    self.cds.Params[0].AsString := FormatDateTime('mm/dd/yy', StrToDate(self.edtFechaIni.EditText));
  self.cds.Open;
end;

constructor TfrmGastosEmpresa.CreateNuevo(AOwner: TComponent; fechaInicial,
  fechaFinal: TDateTime; todos: boolean);
begin
  inherited Create(Aowner);
  self.chbTodos.Checked := todos;
  if todos then
  begin
    self.edtFechaIni.Date := IncYear(now, -1);
    self.edtFechaFin.Date := now;
  end
  else
  begin
    self.edtFechaIni.Date := fechaInicial;
    self.edtFechaFin.Date := fechaFinal;
  end;
  self.ShowModal;
end;

procedure TfrmGastosEmpresa.grdMaterialTitleClick(Column: TColumn);
begin
  self.cds.IndexFieldNames := Column.FieldName;
end;

procedure TfrmGastosEmpresa.edtFiltroChange(Sender: TObject);
begin
  //self.cds.Filtered := False;
  //if (self.edtFiltro.Text <> '') then
  //begin
    self.cds.Filter := 'GASTO LIKE ''%'+self.edtFiltro.Text+'%''';
    self.cds.Filtered := True;
    self.btnCargarClick(Sender);
  //end;
  //self.btnDesfiltrar.Enabled := self.edtFiltro.Text <> '';
end;

procedure TfrmGastosEmpresa.btnExcelClick(Sender: TObject);
begin
  if self.chbTodos.Checked then
    abrirEnExcel(self, self.cds, nil, nil, 'LISTADO DE GASTOS EMPRESA',
    'INTERVALO DEFINIDO: Mostrar Todos')
  else
    abrirEnExcel(self, self.cds, nil, nil, 'LISTADO DE GASTOS EMPRESA',
    'INTERVALO DEFINIDO: del '+DateTimeToStr(self.edtFechaIni.Date)
    +' al '+DateTimeToStr(self.edtFechaFin.Date));
end;

procedure TfrmGastosEmpresa.btnDesfiltrarClick(Sender: TObject);
begin
  self.edtFiltro.Clear;
end;

procedure TfrmGastosEmpresa.chbTodosClick(Sender: TObject);
begin
  self.edtFechaIni.Enabled := not self.chbTodos.Checked;
  self.edtFechaFin.Enabled := not self.chbTodos.Checked;
  self.btnCargarClick(Sender);
end;

procedure TfrmGastosEmpresa.edtFechaIniChange(Sender: TObject);
begin
  self.btnCargarClick(Sender);
end;

end.
